//
//  ZJDyamicModel.m
//  LovingPartyBuilding
//
//  Created by ZJ on 2017/6/6.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJDyamicModel.h"
#import "UILabel+LXAdd.h"

@implementation ZJDyamicModel

- (NSArray<ZJTalkModel *> *)CHILDREN_MODEL{
    return [NSArray yy_modelArrayWithClass:[ZJTalkModel class] json:self.CHILDREN];
}

- (NSString *)description{
    return [self yy_modelDescription];
}

- (CGFloat)dyamicCellHeight{
    CGFloat dyamicCellHeight = 0;
    for (int i = 0; i < self.CHILDREN.count; i ++) {
        ZJTalkModel *model = self.CHILDREN_MODEL[i];
        dyamicCellHeight += model.sectionViewHeight;
        if (model.PARISELIST_MODEL.count > 0) {
            dyamicCellHeight += model.likeCellHeight;
            for (int j = 0; j < model.COMMENTLIST_MODEL.count; j ++) {
                model.indexPath = [NSIndexPath indexPathForRow:j + 1 inSection:0];
                dyamicCellHeight += model.commentCellHeight;
            }
        }else{
            for (int j = 0; j < model.COMMENTLIST_MODEL.count; j ++) {
                model.indexPath = [NSIndexPath indexPathForRow:j inSection:0];
                dyamicCellHeight += model.commentCellHeight;
            }
        }
        
    }
    return dyamicCellHeight;
}

@end

@implementation ZJTalkModel

- (NSArray<ZJImageList *> *)IMGLIST_MODEL{
    return [NSArray yy_modelArrayWithClass:[ZJImageList class] json:self.IMGLIST];
}

- (NSArray<ZJLikeList *> *)PARISELIST_MODEL{
    return [NSArray yy_modelArrayWithClass:[ZJLikeList class] json:self.PARISELIST];
}

- (NSArray<ZJCommentList *> *)COMMENTLIST_MODEL{
    return [NSArray yy_modelArrayWithClass:[ZJCommentList class] json:self.COMMENTLIST];
}

- (NSString *)description{
    return [self yy_modelDescription];
}


- (CGFloat)sectionViewHeight{
    CGFloat nameHeigth = [NSString heightWithTextFont:[UIFont titleTextFont]];
    
    CGFloat textContentLabelHeight = [self.CONTENT getLabelSizeWithLabelFont:[UIFont subTitleTextFont] labelWidth:[UIScreen zj_screenWith] - (12 * 2 + 40 + 10) * [UIScreen zj_screenWScale] labelNumberOfLines:0 labelLineSpace:4 * [UIScreen zj_screenHScale] labelCharacterSpace:1];
    
    CGFloat  sectionViewH = (15 + 5 + 20 + 8 + 8) * [UIScreen zj_screenHScale] + nameHeigth + textContentLabelHeight;
    ///图片间距(上下间距及之间的间距)
    CGFloat imageSpace = 8 * [UIScreen zj_screenWScale];
    
    if (self.IMGLIST.count > 0) {
        //有图片数组
        if (self.IMGLIST_MODEL.count == 1) {

            CGFloat imageH =  ([UIScreen zj_screenWith] - (12 * 2 + 40 + 10 ) * [UIScreen zj_screenWScale])/ 2;
            return sectionViewH + imageH + 2 *imageSpace;
        }else if (self.IMGLIST_MODEL.count == 2) {
          
            ///图片宽度与高度
            CGFloat imageH = ([UIScreen zj_screenWith] - (12 * 2 + 40 + 10 + 40) * [UIScreen zj_screenWScale] - imageSpace) / 2;
            return sectionViewH + imageH + 2 * imageSpace;
        }else {
        
            ///图片宽度与高度
            CGFloat imageH = ([UIScreen zj_screenWith] - (12 * 2 + 40 + 10 + 40) * [UIScreen zj_screenWScale] - imageSpace) / 3;
            NSInteger zheng = self.IMGLIST_MODEL.count / 3;
            NSInteger yu = self.IMGLIST_MODEL.count % 3;
            if (yu > 0) {
                zheng += 1;
            }
            return sectionViewH + (imageH + imageSpace) * zheng + imageSpace;
        }

    }else{
        return  sectionViewH;

    }
}

- (CGFloat)likeCellHeight{
    NSMutableString *contentLabelText = [NSMutableString string];
    [contentLabelText appendFormat:@"    "];
    for (int i = 0; i < self.PARISELIST_MODEL.count; i ++) {
        ZJLikeList *model = self.PARISELIST_MODEL[i];
        if (i == 0) {
            [contentLabelText appendString:model.UNAME];
        }else{
            [contentLabelText appendString:@"、"];
            [contentLabelText appendString:model.UNAME];
        }
    }
    
    CGFloat likeLabelHeight = [contentLabelText getLabelSizeWithLabelFont:[UIFont subTitleTextFont] labelWidth:[UIScreen zj_screenWith] - (62 + 12 + 10) * [UIScreen zj_screenWScale] labelNumberOfLines:2 labelLineSpace:1 labelCharacterSpace:1];
    
    if (!self.COMMENTLIST || self.COMMENTLIST.count == 0) {//没有评论
        
        return (10 + 10 + 8) * [UIScreen zj_screenHScale]  + likeLabelHeight;
    }else{//有评论
        
        return (10 + 10) * [UIScreen zj_screenHScale]  + likeLabelHeight;
    }
    
}

- (CGFloat)commentCellHeight{
    NSMutableString *contentLabelText = [NSMutableString string];
    ZJCommentList *model = [ZJCommentList new];
    if (self.PARISELIST.count > 0) {//有点赞数组
        model = self.COMMENTLIST_MODEL[self.indexPath.row - 1];
    }else{
        model = self.COMMENTLIST_MODEL[self.indexPath.row];
    }
    if (model.FFID) {//被评论人存在
        [contentLabelText appendString:[NSString stringWithFormat:@"%@回复",model.UNAME]];
        [contentLabelText appendString:[NSString stringWithFormat:@"%@: %@",model.FUNAME,model.CONTENT]];
    }else{//没有评论人
        [contentLabelText appendString:[NSString stringWithFormat:@"%@: %@",self.UNAME,model.CONTENT]];
     
    }
    
    CGFloat arrowsHeight = 10 * [UIScreen zj_screenHScale];
    CGFloat contentLabelHeight = [contentLabelText getLabelSizeWithLabelFont:[UIFont subTitleTextFont] labelWidth:[UIScreen zj_screenWith] - (62 + 12 + 10) * [UIScreen zj_screenWScale] labelNumberOfLines:0 labelLineSpace:4 * [UIScreen zj_screenHScale] labelCharacterSpace:1];
    
    if (self.indexPath.row == 0) {//是第一个(有箭头)
        if (self.COMMENTLIST.count == 1) {//只有一个
            return arrowsHeight + contentLabelHeight  + (10 + 8) * [UIScreen zj_screenHScale];
        }else{//是第一个但不是最后一个
            return arrowsHeight + contentLabelHeight  + 10  * [UIScreen zj_screenHScale];
        }
    }else{//不是第一个（没有箭头）
        if (self.PARISELIST.count > 0 && (self.COMMENTLIST.count == self.indexPath.row)) {//有点赞数组时的最有一个
                return contentLabelHeight  + (10 + 8) * [UIScreen zj_screenHScale];
            
        }else  if (! (self.PARISELIST.count > 0) && (self.COMMENTLIST.count == self.indexPath.row + 1)){//无点赞数组时的最有一个
           
                return contentLabelHeight  + (10 + 8) * [UIScreen zj_screenHScale];
    
        }else{//不是最后一个
            return contentLabelHeight + 10 * [UIScreen zj_screenHScale];
        }
    }
}



@end

@implementation ZJImageList

- (NSString *)description{
    return [self yy_modelDescription];
}

@end

@implementation ZJCommentList

- (NSString *)description{
    return [self yy_modelDescription];
}

@end

@implementation ZJLikeList

- (NSString *)description{
    return [self yy_modelDescription];
}

@end
